/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.io.Serializable;
import java.util.List;
import org.hibernate.*;
import pojo.Hanghoa;
import util.HibernateUtil;

/**
 *
 * @author HoaDinhGiang
 */
public class hanghoaDAO {
    
    public static List<Hanghoa> timKiemHangHoa(Hanghoa hh){
       
        List<Hanghoa> ds = null;
         
        int maHang = hh.getMahanghoa();
        String tenHang = hh.getTenhanghoa();
        int maLoai = hh.getLoaihanghoa().getMaloaihanghoa();
        int maNPP = hh.getNhaphanphoi().getManhaphanphoi();
        int maNSX = hh.getNhasanxuat().getManhasanxuat();
        int maDV = hh.getDonvitinh().getMadonvitinh();
        int maKho = hh.getKhohang().getMakhohang();
        double giaban = hh.getGiaban();
        double giaToiThieu = hh.getGiatoithieu();
        double giaToiDa = hh.getGiatoida();
        int hangTon = hh.getHangton();
        
      //  hh.getLoaihanghoa().getMaloaihanghoa();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "select hh from Hanghoa hh where (hh.mahanghoa = "+maHang
                    + "or lower(hh.tenhanghoa) like lower('"+tenHang+"')"
                    + "or hh.loaihanghoa.maloaihanghoa = "+maLoai
                    + "or hh.nhaphanphoi.manhaphanphoi = "+maNPP
                    + "or hh.nhasanxuat.manhasanxuat = "+maNSX
                    + "or hh.donvitinh.madonvitinh = "+maDV
                    + "or hh.giaban = "+giaban
                    + "or hh.giatoithieu = "+giaToiThieu
                    + "or hh.giatoida = "+giaToiDa
                    + "or hh.khohang.makhohang = "+maKho
                    + "or hh.hangton = "+hangTon
                    + ")and hh.kichhoat = true and hh.hangton>0"
                    ;
               //     + "or hh.loaihanghoa.maloaihanghoa = "+maLoai;
            Query query = session.createQuery(hql);
            ds = query.list();
        }catch(HibernateException ex){
            //Log the exception
            System.err.println(ex);
        }finally{
            session.close();
        }
        return ds;
    }
    
   
    public static List<Hanghoa> layDanhSachHangHoaTonTai(){
        List<Hanghoa> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "select ch from Hanghoa ch where ch.kichhoat = true and ch.hangton>0 ORDER BY ch.mahanghoa ASC";
            Query query = session.createQuery(hql);
            ds = query.list();
        }catch(HibernateException ex){
            //Log the exception
            System.err.println(ex);
        }finally{
            session.close();
        }
        return ds;
    }
    public static List<Hanghoa> layDanhSachHangTonCuaKhoHang(int maKho){
        List<Hanghoa> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "select ch from Hanghoa ch where ch.kichhoat = true "
                    + "and ch.hangton>0 and ch.khohang.makhohang = '"+maKho+"' ORDER BY ch.mahanghoa ASC";
            Query query = session.createQuery(hql);
            ds = query.list();
        }catch(HibernateException ex){
            //Log the exception
            System.err.println(ex);
        }finally{
            session.close();
        }
        return ds;
    }
    public static List<Hanghoa> layDanhSachHangTon(){
        List<Hanghoa> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "select ch from Hanghoa ch where ch.kichhoat = true ORDER BY ch.mahanghoa ASC"
                    + "and ch.hangton>0";
            Query query = session.createQuery(hql);
            ds = query.list();
        }catch(HibernateException ex){
            //Log the exception
            System.err.println(ex);
        }finally{
            session.close();
        }
        return ds;
    }
    
    public static List<Hanghoa> layDanhSachMoiHangHoa(){
        List<Hanghoa> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "select ch from Hanghoa ch where ch.kichhoat = true ORDER BY ch.mahanghoa ASC";
            Query query = session.createQuery(hql);
            ds = query.list();
        }catch(HibernateException ex){
            //Log the exception
            System.err.println(ex);
        }finally{
            session.close();
        }
        return ds;
    }
    
    public static Hanghoa layThongTinHangHoa(int maHangHoa){
        Hanghoa hh = null;
        Session session =  HibernateUtil.getSessionFactory().openSession();
        try{
            hh =  (Hanghoa) session.get(Hanghoa.class, maHangHoa);
            Hibernate.initialize(hh.getDonvitinh());
            Hibernate.initialize(hh.getKhohang());
            Hibernate.initialize(hh.getLoaihanghoa());
            Hibernate.initialize(hh.getNhaphanphoi());
            Hibernate.initialize(hh.getNhasanxuat());
        }catch(HibernateException ex){
            //Log the exception
            System.err.println(ex);
        }finally{
            session.close();
        }
        return hh;
    }
 
    public static boolean themHangHoa(Hanghoa hh){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction =  session.beginTransaction();
            Serializable save = session.save(hh);
            transaction.commit();
        } catch (HibernateException ex) {
        //Log the exception
            transaction.rollback();
            System.err.println(ex);
            return false;
        } finally {
        session.close();
        }
        return true;
    }
    
    public static boolean capNhatThongTinHangHoa(Hanghoa hh) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(hh);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
        } finally {
            session.close();
        }
        return true;
}
    public static void xoaTamThoi(Hanghoa hh) {
        /*
        Session session =  HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "update Cuahang ch set kichhoat = false where ch.macuahang = "+maCuaHang;
            System.out.println(hql);
            Query query = session.createQuery(hql);
            int k = query.executeUpdate();
            System.out.print(k);
        }catch(HibernateException ex){
            //Log the exception
            System.err.println(ex);
        }finally{
            session.close();
        }
        * 
        */
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(hh);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
        } finally {
            session.close();
        }
}
     public static boolean xoaVinhVien(int maHangHoa) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Hanghoa hh = hanghoaDAO.layThongTinHangHoa(maHangHoa);
        if(hh==null){
            return false;
        }
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.delete(hh);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
        } finally {
            session.close();
        }
        return true;
}
}
